//
//  AboutViewController.m
//  cyydt
//
//  Created by 959 on 2018/12/24.
//  Copyright © 2018 959. All rights reserved.
//

#import "AboutViewController.h"
#import "CYChangJianWenTiViewController.h"
#import "CYVersionModel.h"
#import "CYVersionUpdateView.h"

@interface AboutViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *versionTableView;
@property (weak, nonatomic) IBOutlet UILabel *appName;
@property (weak, nonatomic) IBOutlet UILabel *appVersion;
@property (nonatomic, strong) CYVersionModel *model;
@end

@implementation AboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.versionTableView.delegate = self;
    self.versionTableView.dataSource = self;
    self.versionTableView.scrollEnabled = NO;
    self.versionTableView.layer.cornerRadius = 10;
    self.versionTableView.layer.masksToBounds = YES;
    self.versionTableView.layer.borderWidth = 0.1;
    self.versionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.versionTableView.layer.borderColor = [UIColor grayColor].CGColor;
    self.title = @"关于创业一点通";
    self.appName.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    self.appVersion.text = [NSString stringWithFormat:@"当前版本：V%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
//}
//在页面消失的时候就让navigationbar还原样式
//-(void)viewWillDisappear:(BOOL)animated{
//
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:nil];
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSArray *cellArr =  @[@[@"版本升级", @"常见问题"]];
    cell.textLabel.text = cellArr[indexPath.section][indexPath.row];
    cell.textLabel.textColor = RGBOF(0x333333);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        [self getVersion];
        
        
    }else if(indexPath.section == 0 && indexPath.row == 1){
        
        CYChangJianWenTiViewController *aboutVC =  [[CYChangJianWenTiViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:aboutVC];
    }
}

- (void)getVersion{
    [EasyShowLodingView showLoding];
//    NSString *url = @"http://easyapi.fx2.cn/easyapi/client?url=apk";
    NSString *url = @"https://flow.api.959.cn/v2/apk";
    NSDictionary *param = @{@"is_ios":@"1"};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取版本信息 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *data = responseResult[@"data"];
                    CYVersionModel *model = [CYVersionModel mj_objectWithKeyValues:data];
                    self.model = model;
                    // app当前版本
                    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
                    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
                    NSLog(@"app_Version ---- %@",app_Version);
                    NSString *versinName = @"";
                    if ([model.version_name hasPrefix:@"v"] || [model.version_name hasPrefix:@"V"]) {
                        versinName = [model.version_name substringFromIndex:1];
                    }else{
                        versinName = model.version_name;
                    }
                    if ([app_Version compare:versinName] == NSOrderedSame ||[app_Version compare:versinName] == NSOrderedDescending) {
                        //不需要更新
                        [self.view makeToast:@"当前版本为最新版本" duration:1.5 position:CSToastPositionCenter];
                    }else{
                        CYVersionUpdateView *view = [[CYVersionUpdateView alloc] initWithVersionModel:self.model];
                        [view show];
                    }

                }
            }else{
                
            }
        }
    }];
}

@end
